#include "DBQuery.h"
#include "TestBase.h"
#include "IDatabase.h"
#include "SQLiteDatabaseDriver.h"

TestBase::TestBase()
{
	info.Name = "SQLiteDatabaseDriver test 1";
	info.Category = 0x0201;
	info.CategoryName = "SQLiteDatabaseDriver";
	info.File = __FILE__;
}

bool TestBase::Run()
{
	SQLiteDatabaseDriver db;
	DatabaseConnectionInfo info;
	DBQuery query;
	IDBQueryResult* res;
	std::string oS1 = "", oS2 = "", eS1 = "John Doe", eS2 = "Jane Doe";
	long oN1 = 0, oN2 = 0, eN1 = 123456789, eN2 = 987654321;
	bool fail = false;
	info.DB = "TestModules/SQLiteDatabaseDriverTest1.db";
	if (!db.Open(&info))
	{
		return false;
	}
	db.BatchExec("TestModules/SQLiteDatabaseDriverTest1.sql");
	query.SetFormatString("select * from test_table");
	res = db.Query(query);
	if (!res)
	{
		db.Close();
		unlink(info.DB.c_str());
		std::cout << GetTestInfo()->Name << " failure info" << std::endl;
		std::cout << "res is NULL" << std::endl;
		return false;
	}
	oS1 = res->GetString("name"); // John Doe
	oN1 = res->GetDoubleWord("num"); // 123456789
	if (res->NextRow())
	{
		oS2 = res->GetString("name"); // Jane Doe
		oN2 = res->GetDoubleWord("num"); // 987654321
	}
	if (oS1 != eS1 || oS2 != eS2 || oN1 != eN1 || oN2 != eN2)
	{
		fail = true;
		std::cout << GetTestInfo()->Name << " failure info" << std::endl;
		if (oS1 != eS1)
		{
			printf("Output          : %s\n", oS1.c_str());
			printf("Expected output : %s\n", eS1.c_str());
		}
		if (oS2 != eS2)
		{
			printf("Output          : %s\n", oS2.c_str());
			printf("Expected output : %s\n", eS2.c_str());
		}
		if (oN1 != eN1)
		{
			printf("Output          : %d\n", oN1);
			printf("Expected output : %d\n", eN1);
		}
		if (oN2 != eN2)
		{
			printf("Output          : %d\n", oN2);
			printf("Expected output : %d\n", eN2);
		}
	}
	delete res;
	db.Close();
	unlink(info.DB.c_str());
	return fail == false;
}